package forms;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class formAddressForResdient extends ActionForm {
	private static final long serialVersionUID = 1L;
	private String state = null;
	private String areaCode = null;
	private String phoneNumber = null;
	private String zipCode = null;
	private String street = null;
	private String city = null;
	
	public String getCity(){
		return city;
	}
	public String getStreet(){
		return street;
	}
	public void setStreet(String street){
		this.street = street;
	}
	public void setCity(String city){
		this.city = city;
	}
	public String getAreaCode() {
		return areaCode;
	}

	public String getState() {
		return state;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public String getZipCode() {
		return zipCode;
	}

	public boolean isNumeric(String name) {
		try {
			Integer.parseInt(name);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean isPhoneNumber(String number) { // xxx-xxxx or xxx xxxx or
													// xxxxxxx will
													// pass the validation
		if (number.length() == 8 && isNumeric(number.substring(0, 2))
				&& isNumeric(number.substring(4, 7))
				&& number.substring(3).equals("-")) {
			return true;
		} else if (number.length() == 8 && isNumeric(number.substring(0, 2))
				&& isNumeric(number.substring(4, 7))
				&& number.substring(3).equals(" ")) {
			return true;
		} else if (number.length() == 7 && isNumeric(number)) {
			return true;
		} else {
			return false;
		}
	}

	public boolean isZipCode(String zipCode) {
		if (zipCode.length() != 5 || !isNumeric(zipCode)) {
			return false;
		} else {
			return true;
		}
	}

	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setCountryCode(null);
		setAreaCode(null);
		setPhoneNumber(null);
		setZipCode(null);
	}

	public void setAreaCode(String areaCode) {
		this.areaCode = areaCode;
	}

	public void setCountryCode(String state) {
		this.state = state;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = zipCode;
	}

	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if(street == null ||street.length() < 1)
			errors.add("firstMsgTag1", new ActionMessage("errors.required",
			"Street"));
		if(city == null ||city.length() < 1)
			errors.add("firstMsgTag1", new ActionMessage("errors.required",
			"City"));
		if(state == null ||state.length() < 1)
			errors.add("firstMsgTag1", new ActionMessage("errors.required",
			"State"));
		if ((areaCode == null) || (areaCode.length() != 3)
				|| (!isNumeric(areaCode))) {
			errors.add("lastMsgTag1", new ActionMessage("errors.required",
					"Area Code"));
		}

		if ((phoneNumber == null) || (phoneNumber.length() < 1)
				|| (!isPhoneNumber(phoneNumber))) {
			errors.add("lastMsgTag1", new ActionMessage("errors.required",
					"Phone Number"));
		}

		if (!isZipCode(zipCode)) {
			errors.add("lastMsgTag1", new ActionMessage("errors.required",
					"Zip Code"));
		}
		return errors;
	}
}
